package lucky.xiong.designPattern;

/**
 * DCL 单例模式
 * double check lock 双重锁定检查
 * 如果不加volatile 还是可能出现指令重拍，所以需要加上。
 */
public class SingletonPattern {
    private volatile static SingletonPattern instance = null;

    private SingletonPattern(){
        System.out.println(Thread.currentThread().getName()+"\t 我是构造方法SingletonPattern()");
    }

    public static SingletonPattern getInstance(){
        if (instance == null){
            synchronized (SingletonPattern.class){
                if (instance == null){
                    instance = new SingletonPattern();
                }
            }
        }
        return instance;
    }

    public static void main(String[] args) {
        for (int i = 0; i < 10; i++) {
            new Thread(()->{
                getInstance();
            },String.valueOf(i)).start();
        }
    }
}
